package com.kevin.controller.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kevin.common.api.PageDTO;
import com.kevin.common.api.R;
import com.kevin.service.entity.SysParams;
import com.kevin.service.query.SysParamsQuery;
import com.kevin.service.service.SysParamsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 参数管理
 *
 * @author 阿沐 babamu@126.com
 */
@RestController
@RequestMapping("sys/params")
@Api(tags = "参数管理")
@AllArgsConstructor
public class SysParamsController {
    private final SysParamsService sysParamsService;

    @GetMapping("page")
    @ApiOperation(value = "分页")
    @SaCheckPermission(value = "sys:params:all")
    public R<PageInfo<SysParams>> page(PageDTO pageDTO, SysParamsQuery query) {
        PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
        return R.ok(new PageInfo<>(sysParamsService.list(Wrappers.lambdaQuery(SysParams.class)
                .like(StrUtil.isNotBlank(query.getParamKey()), SysParams::getParamKey, query.getParamKey())
                .eq(StrUtil.isNotBlank(query.getParamValue()), SysParams::getParamValue, query.getParamValue())
                .eq(query.getParamType() != null, SysParams::getParamType, query.getParamType())
                .orderByDesc(SysParams::getId)
        )));
    }

    @GetMapping("{id}")
    @ApiOperation(value = "信息")
    @SaCheckPermission(value = "sys:params:all")
    public R<SysParams> get(@PathVariable("id") Long id) {
        return R.ok(sysParamsService.getById(id));
    }

    @PostMapping
    @ApiOperation(value = "保存")
    @SaCheckPermission(value = "sys:params:all")
    public R<String> save(@RequestBody SysParams vo) {
        sysParamsService.saveP(vo);

        return R.ok();
    }

    @PutMapping
    @ApiOperation(value = "修改")
    @SaCheckPermission(value = "sys:params:all")
    public R<String> update(@RequestBody @Valid SysParams vo) {
        sysParamsService.update(vo);
        return R.ok();
    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    @SaCheckPermission(value = "sys:params:all")
    public R<String> delete(@RequestBody List<Long> idList) {
        sysParamsService.delete(idList);
        return R.ok();
    }
}
